package com.crizzbe.storeManage.Controller;


import java.util.Iterator;
import java.util.List;

import org.apache.catalina.connector.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.crizzbe.storeManage.entity.Admin;
import com.crizzbe.storeManage.entity.Buyer;
import com.crizzbe.storeManage.entity.User;
import com.crizzbe.storeManage.mapper.BuyerMapper;
import com.crizzbe.storeManage.mapper.UserMapper;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Crizzbe
 * @since 2021-06-22
 */
@RestController
@CrossOrigin
@RequestMapping("/StoreManage/user")
public class UserController {
	@Autowired
	private UserMapper user;
	@GetMapping("/")
	 List<User> index() {
		 return user.selectList(null);
	 }
	@PostMapping("/add")
	public void add(@RequestBody User parm) {
		user.insert(parm);
	}
	@PostMapping("/delete")
	public int deleteData(@RequestBody User parm) {
		 return user.deleteById(parm.Id);
	}
	@GetMapping
	public Boolean isAdmin(@RequestBody Admin role) {
		System.out.print(role.name+role.pass);
          return true;
	}
	@PostMapping("/update")
	
	public void update(@RequestBody User form) {
	user.updateById(form);
	}
	@PostMapping("/login")
	public Boolean login(@RequestBody Admin data) {
		List<User> res=user.selectList(null);
		
		Iterator<User> iter=res.iterator();
		while(iter.hasNext()) {
			User next= iter.next();
		
			
			if(next.uname.equals(data.name)&&next.upassword.equals(data.pass)){
				return true;
			}
		}
		return false;
	}
	@PostMapping("/role")
	public boolean role(@RequestBody Admin data) {
List<User> res=user.selectList(null);
		
		Iterator<User> iter=res.iterator();
		while(iter.hasNext()) {
			User next= iter.next();
		
			
			if(next.uname.equals(data.name)&&next.upassword.equals(data.pass)){
				if(next.role.equals("admin")) return true;
			}
		}
		return false;
	}
}

